#import "FlutterRpsdkPlugin.h"
#import <RPSDK/RPSDK.h>

@implementation FlutterRpsdkPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"flutter_rpsdk"
            binaryMessenger:[registrar messenger]];
  FlutterRpsdkPlugin* instance = [[FlutterRpsdkPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  NSString *method = call.method;
  NSDictionary *params = call.arguments;
  NSLog(@"plugin handleMethodCall: %@, argus: %@", method, params);

  if ([@"init" isEqualToString:method]) {
    [RPSDK setup];
    result(nil);
  } else if([@"start" isEqualToString:method]) {
    NSDictionary *config = [self dictionaryFromArguments:params key:@"config"];
    NSString *token = [self stringFromArguments:params key:@"token"];

    [RPSDK startWithVerifyToken:token
      viewController:self
      completion:^(RPResult * _Nonnull result) {
        // 建议接入方调用实人认证服务端接口DescribeVerifyResult，
        // 来获取最终的认证状态，并以此为准进行业务上的判断和处理。
        NSLog(@"实人认证结果：%@", result);
        result(@{@"state": @(result.state), @"message": result.message, 'code': @(result.errorCode)});        
    }];

  } else if([@"startByNative" isEqualToString:method]) {
    [RPSDK startByNativeWithVerifyToken:token
      viewController:self
      completion:^(RPResult * _Nonnull result) {
        // 建议接入方调用实人认证服务端接口DescribeVerifyResult，
        // 来获取最终的认证状态，并以此为准进行业务上的判断和处理。
        NSLog(@"实人认证结果：%@", result);
        result(@{@"state": @(result.state), @"message": result.message, 'code': @(result.errorCode)});        
    }];
  } else {
    result(FlutterMethodNotImplemented);
  }
}

- (NSString *)stringFromArguments:(NSDictionary *)params key:(NSString *)key {
    if (![params isKindOfClass:[NSDictionary class]]) {
        return nil;
    }

    NSString *value = [params valueForKey:key];
    if (![value isKindOfClass:[NSString class]]) {
        return nil;
    } else {
        return value;
    }
}

- (NSInteger)intFromArguments:(NSDictionary *)params key:(NSString *)key {
    if (![params isKindOfClass:[NSDictionary class]]) {
        return 0;
    }

    NSNumber *value = [params valueForKey:key];
    if (![value isKindOfClass:[NSNumber class]]) {
        return 0;
    } else {
        return [value integerValue];
    }
}

- (double)doubleFromArguments:(NSDictionary *)params key:(NSString *)key {
    if (![params isKindOfClass:[NSDictionary class]]) {
        return 0;
    }

    NSNumber *value = [params valueForKey:key];
    if (![value isKindOfClass:[NSNumber class]]) {
        return 0;
    } else {
        return [value doubleValue];
    }
}

- (BOOL)boolFromArguments:(NSDictionary *)params key:(NSString *)key {
    if (![params isKindOfClass:[NSDictionary class]]) {
        return NO;
    }

    NSNumber *value = [params valueForKey:key];
    if (![value isKindOfClass:[NSNumber class]]) {
        return NO;
    } else {
        return [value boolValue];
    }
}

- (NSDictionary *)dictionaryFromArguments:(NSDictionary *)params key:(NSString *)key {
    if (![params isKindOfClass:[NSDictionary class]]) {
        return nil;
    }

    NSDictionary *value = [params valueForKey:key];
    if (![value isKindOfClass:[NSDictionary class]]) {
        return nil;
    } else {
        return value;
    }
}

@end
